<?


require_once  $_SERVER['DOCUMENT_ROOT']."/../_lib/base/common.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.newsletter.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.template.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.holidays.php";


	$template = new template();
	$template->title = "Special offers newsletter";
	$template->section_highlight = "home";
	$template->left_col = true;
	$template->breadcrumbs_html = "<li>&rsaquo;</li><li title=\"Special offers newsletter\"><a href=\"/newsletter.html\" title=\"Special offers newsletter\">Newsletter</a></li>";	
	$template->ld_head();


// FUNCTION // Display send form
function display_form($form_data=''){


	
?>


<h2 title="Newsletter">Newsletter</h2>

<h4>Subscribe to the newsletter</h4>

<img class="photo_right" src="/images/random_beachball.jpg" alt="Beachball" />

<p>Sign up and get our best holiday deals and last minute offers sent straight to you.</p>



<?
if($GLOBALS['note']){
	echo display_message($GLOBALS['note']);
}
?>


	
<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>" name="newsletter_form" >
<input type="hidden" name="action" value="validate"/>
<table>
<tr>
<th width="120">Email address</th>
<td><input type="text" name="epost" value="<?= $form_data['epost'] ?>"/></td>
<td><div style="margin-left: 10px; text-align: center"><input type="image" src="/images/button_go_white.gif" value="submit" /></div></td>
</tr>
</table>
	
</form>


<br />


<p>If you wish to be removed from our mailing list please click <a href="<?= $_SERVER['PHP_SELF'] ?>?action=unsub">here</a>.</p>
	

	
<?
}
// END FUNCTION




// END FUNCTION
function display_unsub_form($form_data=""){
?>






<h2 title="Newsletter">Newsletter</h2>



<img class="photo_right" src="/images/random_beachball.jpg" alt="Beachball" />

<h4>Unsubscribe from our mailing list</h4>

<p>Enter your email address that you wish to remove. </p>


<?
if($GLOBALS['note']){
	echo display_message($GLOBALS['note']);
}
?>


	<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>" name="newsletter_form" >
	<input type="hidden" name="action" value="unsub_validate"/>
		<table>
			<tr>
				<th width="110">Email address:</th>
				<td><input type="text" name="epost" value="<?= $form_data['epost'] ?>"/></td>
				<td>
<div style="margin-left: 10px; text-align: center"><input type="image" src="/images/button_go_white.gif" value="submit" /></div>
</td>
</tr>
</table>

	
	</form>
	

	
<?
}






// FUNCTION
function display_confirmation($params){


?>



<h2 title="Newsletter">Newsletter</h2>


<img class="photo_right" src="/images/random_beachball.jpg" alt="Beachball" />

<?
if($GLOBALS['note']){
	echo display_message($GLOBALS['note']);
}
?>


<h4>Thank you</h4>
<p>Thank you for registering the email address <strong><?= $params['email'] ?></strong>. You'll soon receive our regular offers by email.</p>

<ul class="list_qlinks">
<li><a href="/index.html">Go to the home page</a></li>
</ul>



<?
}








//FUNCTION
function display_confirmation_remove($params){
?>


<h2 title="Newsletter">Newsletter</h2>


<img class="photo_right" src="/images/random_beachball.jpg" alt="Beachball" />

<?
if($GLOBALS['note']){
	echo display_message($GLOBALS['note']);
}
?>



<h4>Thank you</h4>

<p>The email address <strong><?= $params['email'] ?></strong> has been removed from our mailing list.</p>

<ul class="list_qlinks">
<li><a href="/index.html">Go to the home page</a></li>
</ul>


<?
}
// END FUNCTION


switch ($_REQUEST['action']){
	default:
		display_form();
	break;
	case "validate";
		$newsletter = new newsletter;
		$new_form_data = process_form_data($_POST);
		if($errors_found = $newsletter->validate_form($new_form_data)){
			$my_message = array();
			$my_message['type'] = 'neg';
			$my_message['message'] = "There were problems with the form";
			$my_message['list'] = $errors_found;
			$GLOBALS['note'] = $my_message;
			$form_data = unprocess_form_data($new_form_data);
			display_form($form_data);
		}else{
			$newsletter->insert_email($new_form_data);
			$my_message = array();
			$my_message['type'] = 'pos';
			$my_message['message'] = "Email address subscribed";
			$GLOBALS['note'] = $my_message;
			display_confirmation($new_form_data);
		}
	break;
	case "unsub";
		display_unsub_form();
	break;
	case "unsub_validate";
		$newsletter = new newsletter;
		$new_form_data = process_form_data($_POST);
		if($errors_found = $newsletter->validate_form_unsub($new_form_data)){
			$my_message = array();
			$my_message['type'] = 'neg';
			$my_message['message'] = "There were problems with the form";
			$my_message['list'] = $errors_found;
			$GLOBALS['note'] = $my_message;
			$form_data = unprocess_form_data($new_form_data);
			display_unsub_form($form_data);
		}else{
			$newsletter->delete_email($new_form_data);
			$my_message = array();
			$my_message['type'] = 'pos';
			$my_message['message'] = "Email address removed from our mailing list";
			$GLOBALS['note'] = $my_message;
			display_confirmation_remove($new_form_data);
		}
	break;
}

function process_form_data($form_data){
	
	$new_array = array();
	
	$new_array['email'] = $form_data['epost'];
	$new_array['user_ip'] = $_SERVER['REMOTE_ADDR'];
	
	return $new_array;

}

function unprocess_form_data($form_data){
	
	$new_array = array();
	
	$new_array['epost'] = $form_data['email'];
	
	return $new_array;

}


$template->ld_foot();

?>				